node -v
if [ $? -eq 0 ]; then
  echo " *** node installed, skip install"
else
  echo " *** node not installed, start install"
  wget https://nodejs.org/dist/v14.17.6/node-v14.17.6-linux-x64.tar.xz
  tar -xvf node-v14.17.6-linux-x64.tar.xz
  rm -f node-v14.17.6-linux-x64.tar.xz
  mv node-v14.17.6-linux-x64.tar.xz /usr/local/node
  ln -s /usr/local/node/bin/node /usr/local/bin/node
  ln -s /usr/local/node/bin/npm /usr/local/bin/npm
fi

echo " *** build web and copy to public/"
# shellcheck disable=SC2164
cd web_src

vue -V
if [ $? -eq 0 ]; then
  echo " *** vue cli installed, skip install"
else
  echo " *** vue cli not installed, start install"
  npm install -g vue
fi

npm i
npm run build:prod

# 修改前先判断在public/index.html里有没有包含 missile.js， 如果没有就执行下面的命令
echo "修改index.html文件，在 </body>前添加播放h265的js依赖 <script type='text/javascript' src='static/js/missile.js'></script> <script src='static/js/h265webjs-v20221106.js'></script>"
if ! grep -q "missile.js" public/index.html; then
  sed -i 's@</body>@<script type="text/javascript" src="static/js/missile.js"></script> <script src="static/js/h265webjs-v20221106.js"></script>@' public/index.html
fi


echo " *** 安装后端程序依赖"
cd ..
npm i

# 从命令行参数取是否要安装apidoc

apidoc_flag=false

# 遍历所有命令行参数
while [[ $# -gt 0 ]]; do
    case "$1" in
        --apidoc)
            apidoc_flag=true
            ;;
        *)
            # 未知的选项
            echo "unknown option: $1"
            exit 1
            ;;
    esac
    shift
done

# 检查 --apidoc 是否存在
if [ "$apidoc_flag" = true ]; then
  if command -v apidoc &> /dev/null; then
    echo " *** apidoc installed, skip install"
  else
    echo " ** apidoc not installed, start install"
    npm install apidoc@0.25 -g
  fi

  echo " *** 生成api文档并拷贝到 public/ 目录"
  apidoc -i routers/ -o public/apidoc/
fi

if [ ! -d "GraphicsMagick-1.3.21" ]; then
  echo " *** install GraphicsMagick lib"

  wget http://ftp.icm.edu.pl/pub/unix/graphics/GraphicsMagick/1.3/GraphicsMagick-1.3.21.tar.gz
  tar zxvf GraphicsMagick-1.3.21.tar.gz
  rm -f GraphicsMagick-1.3.21.tar.gz
  # shellcheck disable=SC2164
  cd GraphicsMagick-1.3.21/
  ./configure
  make -j8
  make install
  gm version
  # shellcheck disable=SC2103
  cd ..
else
  echo "*** GraphicsMagick lib installed, skip install"
fi

if [ ! -f "data/config.js" ]; then
  echo "config.js not exist, copy from db/config.js"
  mkdir data/
  cp db/config.js data/config.js
fi

echo " check pm2 is installed"
#!/bin/bash

# Check if pm2 is already installed
if command -v pm2 &> /dev/null
then
    echo "pm2 is already installed. Skipping installation."
else
    # Install pm2 using npm
    npm install pm2 -g

    # Verify the installation
    if command -v pm2 &> /dev/null
    then
        echo "pm2 has been successfully installed, configuring it."
        pm2 install pm2-logrotate
        pm2 set pm2-logrotate:retain 50
    else
        echo "Failed to install pm2. Please check your npm and Node.js installation."
    fi
fi

echo " *******************************************"
echo " * 启用方式："
echo " * node gb28181_app.js"
echo " * 或者使用: pm2 start gb28181_app.js"
